<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Msg\Send;
use App\Models\WechatMsgList;
use App\Models\WechatTemplate;
use App\Models\WechatTemplateMsg;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class WechatMsgListController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '消息列表';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new WechatMsgList());

        $grid->column('id', __('ID'));
//        $grid->column('tpl_id', __('模版ID'));
        $grid->column('tpl_name', __('模版名称'));
        $grid->column('tpl_content', __('消息内容'));
        $states = [
            'on'  => ['value' => 1, 'text' => '正常', 'color' => 'success'],
            'off' => ['value' => 0, 'text' => '禁用', 'color' => 'danger'],
        ];
        $grid->column('records', '成功数')->display(function ($records) {

            return WechatTemplateMsg::query()->where('msg_id', $this->id)->where('state', 1)->count();
        })->label();
        $grid->column('state', __('状态'))->switch($states);
        $grid->column('create_user', __('创建人'));
        $grid->column('create_time', __('创建时间'));
        $grid->column('update_time', __('更新时间'));
        $grid->model()->orderBy('id', 'desc');
        $grid->filter(function ($filter) {
            $filter->disableIdFilter();
            $filter->like('tpl_name', __('模版标题'))->placeholder('模版标题');
        });
        $grid->actions(function ($actions) {
            $actions->add(new Send());
        });
        $grid->disableExport();
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(WechatMsgList::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('tpl_id', __('模版ID'));
        $show->field('tpl_name', __('模版标题'));
        $show->tpl_content()->json();
        $show->field('state', __('状态'));
        $show->field('create_user', __('创建人'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new WechatMsgList());
        $form->display('id', __('ID'));
//        $form->table('tpl_content1', __('消息内容1'),function ($table) {
//            $table->text('key');
//            $table->text('value');
//            $table->text('desc');
//    });
        $tplList = WechatTemplate::query()->where('is_del', 0)->orderBy('sort')->pluck('tpl_name', 'tpl_id');
        $tplId1 = '7TgRt75MhzMOdw8znXUwEIT9fVOIVgeO9hEI3bKpPnA';// 工单取消提醒-工单取消通知
        $tplId2 = 'uFtb_fubcm16rTDJjKCbb6CWEFC7Tw4nim7uuyuWbd0';// 工单催办提醒-工单催办通知
        $tplId3 = 'o6tSsxp_cyVeuVc-Dpl4lncstpHbA-dcjA36ycuE4xM';// 工单处理完成通知-佣金结算通知
        $tplId4 = 'bAt8K3PsZynWg75DR4lQVLpSsvCAnjoIx5LxLrOfYx0';// 工单待处理通知-项目上新通知
        $tplId5 = 'gv8Vd2WJDdsqFMTk7KIaa1FXYOB9O4SbZjQcA-f6bgM';// 工单分配完成通知-工单分配完成通知
        $form->select('tpl_id', __('模版库'))->options($tplList)
            ->when($tplId1, function (Form $form) {
                $form->embeds('tpl_content', '消息内容', function ($form) {
                    $form->text('thing6', '工单名称');
                    $form->radio('const4', '取消原因')->options(
                        ['项目临停' => '项目临停', '任务已完成' => '任务已完成',]
                    )->default('项目临停');
                    $form->date('time5', '取消时间')->format('YYYY年MM月DD日')->default(date('Y年m月d日'));
                });

            })->when($tplId2, function (Form $form) {
                $form->embeds('tpl_content', '消息内容', function ($form) {
                    $form->text('thing1', '工单名称');
                    $form->text('thing6', '项目名称');
                    $form->radio('const14', '紧急程度')->options(
                        ['一般' => '一般', '紧急' => '紧急', '特急' => '特急',]
                    )->default('一般');
                    $form->datetime('time12', '截止时间')->format('YYYY年MM月DD日 HH:mm:ss')->default(date('Y年m月d日 H:i:s'));
                })->rules('required', [
                    'required' => '请填写内容',
                ]);

            })->when($tplId3, function (Form $form) {
                $form->embeds('tpl_content', '消息内容', function ($form) {
                    $form->text('thing2', '工单名称');
                    $form->radio('const5', '当前状态')->options(
                        ['佣金结算' => '佣金结算', '预付结算' => '预付结算', '款项已结算至支付宝' => '款项已结算至支付宝']
                    )->default('佣金结算');
                    $form->datetime('time4', '完成时间')->format('YYYY年MM月DD日')->default(date('Y年m月d日'));;
                })->rules('required', [
                    'required' => '请填写内容',
                ]);

            })->when($tplId4, function (Form $form) {
                $form->embeds('tpl_content', '消息内容', function ($form) {
                    $form->text('thing15', '工单名称');
                    $form->text('thing9', '通知事项');
                    $form->datetime('time5', '时间')->format('YYYY年MM月DD日 HH时')->default(date('Y年m月d日 H时'));
                })->rules('required', [
                    'required' => '请填写内容',
                ]);
            })->when($tplId5, function (Form $form) {
                $form->embeds('tpl_content', '消息内容', function ($form) {
                    $form->text('thing2', '工单名称');
                    $form->text('thing1', '客户名称');
                    $form->datetime('time6', '分配时间')->format('YYYY年MM月DD日 HH:mm:ss')->default(date('Y年m月d日 H:i:s'));
                })->rules('required', [
                    'required' => '请填写内容',
                ]);
            });

        $form->textarea('url', __('查看详情链接地址'))->rules('nullable|url', [
            'url' => '请输入有效链接地址，需包含http或https'
        ])->default("");
        $form->switch('state', __('状态'))->default(1);
        $form->hidden('create_user');
        $form->hidden('tpl_name');
        // 获取当前登录用户

        //保存前回调
        $form->saving(function (Form $form) {
            $user = auth()->guard('admin')->user();
            $form->create_user = $user->name;
            $form->tpl_name = WechatTemplate::query()->where('tpl_id', $form->tpl_id)->value('tpl_name');
        });


        $form->footer(function ($footer) {

            // 去掉`重置`按钮
            $footer->disableReset();

            // 去掉`提交`按钮
//            $footer->disableSubmit();

            // 去掉`查看`checkbox
            $footer->disableViewCheck();

            // 去掉`继续编辑`checkbox
            $footer->disableEditingCheck();

            // 去掉`继续创建`checkbox
            $footer->disableCreatingCheck();

        });

        return $form;
    }
}
